package com.ailanyin.modules.file.controller;

import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.modules.file.domain.SysFile;
import com.ailanyin.modules.file.service.SysFileService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 11:17:58
 */
@RestController
@RequestMapping("/common")
@Api(tags = "文件管理")
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;

    @PostMapping
    @ApiOperation(value = "新增('business:sysFile:add')")
    @PreAuthorize("hasAuthority('business:sysFile:add')")
    public AjaxResult insertSysFile(@RequestBody SysFile sysFile) {
        sysFileService.insertSysFile(sysFile);
        return AjaxResult.success();
    }

    @PutMapping
    @ApiOperation(value = "修改('business:sysFile:edit')")
    @PreAuthorize("hasAuthority('business:sysFile:edit')")
    public AjaxResult updateSysFile(@RequestBody SysFile sysFile) {
        sysFileService.updateSysFile(sysFile);
        return AjaxResult.success();
    }

    @DeleteMapping
    @ApiOperation(value = "批量删除('business:sysFile:remove')")
    @PreAuthorize("hasAuthority('business:sysFile:remove')")
    public AjaxResult deleteByIds(@RequestBody BaseQuery<Long> query) {
        sysFileService.deleteByIds(query.getIds());
        return AjaxResult.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取列表('business:sysFile:list')")
    @PreAuthorize("hasAuthority('business:sysFile:list')")
    public CommonPage list(SysFile sysFile,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysFile> list = sysFileService.getListByParam(sysFile);
        return CommonPage.restPage(list);
    }

    @GetMapping(value = {"/{id}"})
    @ApiOperation(value = "根据id获取详情('business:sysFile:query')")
    @PreAuthorize("hasAuthority('business:sysFile:query')")
    public AjaxResult getInfo(@PathVariable(value = "id" , required = false) Long id) {
        return AjaxResult.success(sysFileService.getInfo(id));
    }

    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public AjaxResult fileUpload(MultipartFile file) {
        return sysFileService.upload(file);
    }
}
